package com.wgz.ssm.controller;

import com.wgz.ssm.bean.Dept;
import com.wgz.ssm.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author wgz
 * Created by admin on 2021/3/30
 */
@Controller
public class DeptController {
    @Autowired
    DeptService deptService;
    @GetMapping("/dept/list")
    public String list(Model model){
        List<Dept> allDept = deptService.findAllDept();

        model.addAttribute("list",allDept);
        return "dept/list";
    }
    @GetMapping("/dept/insertUI")
    public String insertUI( ){

        return "dept/insert";
    }


    @PostMapping("/dept/insert")
    public String insert( Dept dept){
        deptService.insert(dept);
        return "redirect:/dept/list";
    }

    @GetMapping("/dept/updateUI")
    public String updateUI( Integer id ,Model model){
        //查询 dept
        Dept dept = deptService.findDeptById(id);
        // modle
        model.addAttribute("dept",dept);
        return "dept/update";
    }



    @PostMapping("/dept/update")
    public String update( Dept dept){
        //修改
        deptService.updateDept(dept);

        return "redirect:/dept/list";
    }

    @GetMapping("/dept/del")
    public String del( Integer id){
        //删除
        deptService.delDept(id);

        return "redirect:/dept/list";
    }

    @GetMapping("/dept/getlist")
    @ResponseBody
    public List<Dept> getList( ){
        return deptService.findAllDept();
    }

    @GetMapping("/dept/{id}")
    @ResponseBody
    public Dept getList(@PathVariable( name = "id") Integer id ){
        return deptService.findDeptById(id);
    }
}
